package com.yangyuhao.haowuyoupin.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yangyuhao.haowuyoupin.ProductDetailActivity;
import com.yangyuhao.haowuyoupin.R;
import com.yangyuhao.haowuyoupin.model.Product;
import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_ITEM = 0;
    public static final int TYPE_FOOTER = 1;
    
    private Context context;
    private List<Product> products;
    private boolean showFooter = false;

    public ProductAdapter(Context context, List<Product> products) {
        this.context = context;
        this.products = products;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_footer, parent, false);
            return new FooterViewHolder(view);
        }
        View view = LayoutInflater.from(context).inflate(R.layout.item_product, parent, false);
        return new ProductViewHolder(view, this);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ProductViewHolder) {
            Product product = products.get(position);
            ProductViewHolder productHolder = (ProductViewHolder) holder;
            
            int imageResId = context.getResources().getIdentifier(
                product.getImage(), "drawable", context.getPackageName());
            productHolder.imageView.setImageResource(imageResId);
            productHolder.titleView.setText(product.getTitle());
            productHolder.priceView.setText(product.getPrice());
            productHolder.unitView.setText(product.getUnit());

            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent(v.getContext(), ProductDetailActivity.class);
                intent.putExtra(ProductDetailActivity.EXTRA_PRODUCT_IMAGE, product.getImage());
                intent.putExtra(ProductDetailActivity.EXTRA_PRODUCT_TITLE, product.getTitle());
                intent.putExtra(ProductDetailActivity.EXTRA_PRODUCT_PRICE, product.getPrice());
                intent.putExtra(ProductDetailActivity.EXTRA_PRODUCT_UNIT, product.getUnit());
                intent.putExtra(ProductDetailActivity.EXTRA_PRODUCT_DESCRIPTION, product.getDescription());
                v.getContext().startActivity(intent);
            });
        }
    }

    @Override
    public int getItemCount() {
        return products.size() + (showFooter ? 1 : 0);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == products.size() && showFooter) {
            return TYPE_FOOTER;
        }
        return TYPE_ITEM;
    }

    public void showFooter(boolean show) {
        if (showFooter != show) {
            showFooter = show;
            notifyDataSetChanged();
        }
    }

    static class ProductViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView titleView;
        TextView priceView;
        TextView unitView;
        
        private final ProductAdapter adapter;

        ProductViewHolder(View view, ProductAdapter adapter) {
            super(view);
            this.adapter = adapter;
            imageView = view.findViewById(R.id.product_image);
            titleView = view.findViewById(R.id.product_title);
            priceView = view.findViewById(R.id.product_price);
            unitView = view.findViewById(R.id.product_unit);
        }
    }

    static class FooterViewHolder extends RecyclerView.ViewHolder {
        FooterViewHolder(View view) {
            super(view);
        }
    }
} 